package com.hbnu.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.mapper.PapertypeMapper;
import com.hbnu.system.model.entity.Paper_Type;
import com.hbnu.system.model.vo.ChildrenNameId;
import com.hbnu.system.model.vo.NameId;
import com.hbnu.system.service.IPaperTypeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PaperTypeServiceImpl extends ServiceImpl<PapertypeMapper, Paper_Type> implements IPaperTypeService {

    @Override
    public List<NameId> getCascadingData() {
        List<Paper_Type> papertypeList = baseMapper.selectList(Wrappers.lambdaQuery(Paper_Type.class));

        // 1. 一级类型: Type1ID -> 名称 (Type1ID唯一)
        Map<Integer, String> type1Map = new HashMap<>();
        // 2. 二级类型: Type2ID -> 名称 (Type2ID唯一)，关联一级ID: Type2ID -> ConnectionID1 (对应一级Type1ID)
        Map<Integer, String> type2Map = new HashMap<>();
        Map<Integer, Integer> type2ToType1 = new HashMap<>();
        // 3. 三级类型: 用记录主键ID作为唯一标识(解决Type3ID重复问题)，关联二级ID: 记录ID -> ConnectionID2 (对应二级Type2ID)
        Map<Integer, String> type3Map = new HashMap<>(); // key: 记录主键ID
        Map<Integer, Integer> type3ToType2 = new HashMap<>(); // key: 记录主键ID, value: 关联的二级Type2ID

        for (Paper_Type pt : papertypeList) {
            // 收集一级类型
            type1Map.putIfAbsent(pt.getType1ID(), pt.getPaperType1());

            // 收集二级类型 (排除"无"且Type2ID有效)
            if (!"无".equals(pt.getPaperType2()) && pt.getType2ID() != 0) {
                type2Map.putIfAbsent(pt.getType2ID(), pt.getPaperType2());
                type2ToType1.putIfAbsent(pt.getType2ID(), pt.getConnectionID1());
            }

            // 收集三级类型 (排除"无"且Type3ID有效)
            if (!"无".equals(pt.getPaperType3()) && pt.getType3ID() != 0) {
                int recordId = pt.getPaperTypeID(); // 用主键ID作为三级唯一标识
                int type3Id = pt.getType3ID();       // 业务ID，用于前端 value
                type3Map.putIfAbsent(recordId, pt.getPaperType3());
                type3ToType2.putIfAbsent(recordId, pt.getConnectionID2());

                // 将三级节点添加到对应二级节点的子列表，使用 Type3ID 作为 value
                type3Map.putIfAbsent(recordId, pt.getPaperType3());
                type3ToType2.putIfAbsent(recordId, pt.getConnectionID2());
            }
        }

        // 构建三级 -> 二级的子节点映射
        Map<Integer, List<NameId>> type2Children = new HashMap<>();
        for (Map.Entry<Integer, String> type3Entry : type3Map.entrySet()) {
            Integer recordId = type3Entry.getKey(); // 三级节点唯一ID(主键)
            String type3Name = type3Entry.getValue();
            Integer parentType2Id = type3ToType2.get(recordId); // 关联的二级Type2ID

            // 处理名称前缀 (修正"人文科学-"的判断，去除前缀)
            if (type3Name.contains("自然科学-")) {
                type3Name = type3Name.substring(5); // "自然科学-"共5个字符
            } else if (type3Name.contains("人文科学-")) {
                type3Name = type3Name.substring(5); // "人文科学-"共5个字符
            }

            // 获取 Type3ID 作为 value
            int type3Id = papertypeList.stream()
                    .filter(pt -> pt.getPaperTypeID() == recordId)
                    .map(Paper_Type::getType3ID)
                    .findFirst()
                    .orElse(0);

            // 将三级节点添加到对应二级节点的子列表，使用 Type3ID 作为 value
            type2Children.computeIfAbsent(parentType2Id, k -> new ArrayList<>())
                    .add(new NameId(type3Name, type3Id));
        }

        // 构建二级 -> 一级的子节点映射
        Map<Integer, List<NameId>> type1Children = new HashMap<>();
        for (Map.Entry<Integer, String> type2Entry : type2Map.entrySet()) {
            Integer type2Id = type2Entry.getKey(); // 二级节点ID(Type2ID)
            String type2Name = type2Entry.getValue();
            Integer parentType1Id = type2ToType1.get(type2Id); // 关联的一级Type1ID

            // 获取当前二级节点的三级子节点
            List<NameId> thirdLevel = type2Children.getOrDefault(type2Id, new ArrayList<>());

            // 构建二级节点(有子节点则为ChildrenNameId，否则为NameId)
            if (thirdLevel.isEmpty()) {
                type1Children.computeIfAbsent(parentType1Id, k -> new ArrayList<>())
                        .add(new NameId(type2Name, type2Id));
            } else {
                ChildrenNameId secondLevel = new ChildrenNameId(type2Name, type2Id);
                secondLevel.setChildren(thirdLevel);
                type1Children.computeIfAbsent(parentType1Id, k -> new ArrayList<>())
                        .add(secondLevel);
            }
        }

        // 构建最终的一级节点列表
        List<NameId> result = new ArrayList<>();
        for (Map.Entry<Integer, String> type1Entry : type1Map.entrySet()) {
            Integer type1Id = type1Entry.getKey(); // 一级节点ID(Type1ID)
            String type1Name = type1Entry.getValue();

            // 获取当前一级节点的二级子节点
            List<NameId> secondLevel = type1Children.getOrDefault(type1Id, new ArrayList<>());

            // 构建一级节点(有子节点则为ChildrenNameId，否则为NameId)
            if (secondLevel.isEmpty()) {
                result.add(new NameId(type1Name, type1Id));
            } else {
                ChildrenNameId firstLevel = new ChildrenNameId(type1Name, type1Id);
                firstLevel.setChildren(secondLevel);
                result.add(firstLevel);
            }
        }

        return result;
    }
}
